package com.ch.forum.controller;

import com.alibaba.fastjson.JSON;
import com.ch.forum.bo.ResponseResult;
import com.ch.forum.model.UserModel;
import com.ch.forum.service.UserService;
import com.ch.forum.utils.StringUtils;
import com.ch.forum.utils.VerificationCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RequestMapping("/user")
@Controller
public class UserController {

    public static final String INDEX_PAGE = "user/index";

    public static final String CHECK_PAGE = "user/check";

    @Autowired
    private UserService userService;

    @GetMapping(value = "/index")
    public String index(){
        return INDEX_PAGE;
    }

    @GetMapping(value = "/check")
    public String check(){
        return CHECK_PAGE;
    }

    @GetMapping(value = "/check/list/{pageNo}/{pageSize}")
    @ResponseBody
    public ResponseResult checkListPage(@PathVariable("pageNo") int pageNo,
                                        @PathVariable("pageSize") int pageSize){
        return userService.checkListPage(pageNo,pageSize);
    }

    @PostMapping(value = "/valid")
    @ResponseBody
    public ResponseResult valid(@RequestParam("ids") String ids){
        return userService.checkUser(ids);
    }

    @GetMapping("/check-valid/{id}")
    @ResponseBody
    public ResponseResult checkValid(@PathVariable("id")Long id){
        return userService.checkValid(id);
    }

    @PutMapping(value = "/vip/{ids}")
    @ResponseBody
    public ResponseResult vipUser(@PathVariable String ids){
        return userService.vipUser(ids);
    }

    @PostMapping()
    @ResponseBody
    public ResponseResult addUser(String data){
        if(!StringUtils.isEmpty(data)){
            return userService.addUser(JSON.parseObject(data, UserModel.class));
        }else {
            return new ResponseResult(false, "参数不能为空");
        }
    }

    @GetMapping(value = "/sms/{no}")
    @ResponseBody
    public void sms(@PathVariable String no, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        VerificationCodeUtils verificationCodeUtils = new VerificationCodeUtils();
        verificationCodeUtils.showCheckCode(no, request, response);
    }

    @PostMapping(value = "/login")
    @ResponseBody
    public ResponseResult login(String phone, String pwd){
        return userService.login(phone, pwd);
    }

    @GetMapping(value = "/forget/{phone}")
    @ResponseBody
    public ResponseResult forget(@PathVariable String phone){
        return userService.forget(phone);
    }

    @PostMapping("/update-pwd/{phone}")
    @ResponseBody
    public ResponseResult updatePwd(@PathVariable("phone")String phone,
                                    @RequestParam("old")String old,
                                    @RequestParam("latest")String latest){
        return userService.updatePwd(phone, old, latest);
    }

    @PostMapping("/update-user/{userId}")
    @ResponseBody
    public ResponseResult updateUserInfo(@PathVariable("userId")Long userId,
                                         @RequestParam("phone")String phone,
                                         @RequestParam("nickName")String nickName,
                                         @RequestParam("imgUrl")String imgUrl){
        return  userService.updateUserInfo(userId, nickName, imgUrl, phone);
    }

    @PostMapping("/check-deal-pwd")
    @ResponseBody
    public ResponseResult checkDealPwd(Long id, String dealPwd){
        return userService.checkDealPwd(id, dealPwd);
    }

    @GetMapping("/user-lists")
    @ResponseBody
    public ResponseResult findUserLists(){
        return userService.findUserLists();
    }

    @GetMapping("/info/{id}")
    @ResponseBody
    public ResponseResult findUserById(@PathVariable("id")Long id){
        return userService.findUserById(id);
    }
}
